<template>
  <div class="app-container">
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="98px">
      <el-form-item label="患者姓名" prop="patientName">
        <el-input
            v-model="queryParams.patientName"
            placeholder="请输入患者姓名"
            clearable
            size="small"
            style="width:240px"
        />
      </el-form-item>
      <el-form-item label="挂号单号" prop="regId">
        <el-input
            v-model="queryParams.regId"
            placeholder="请输入挂号单号"
            clearable
            size="small"
            style="width:240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 数据表格开始 -->
    <el-table v-loading="loading" border :data="orderChargeTableList">
      <el-table-column label="订单号" align="center" width="210" prop="orderId"/>
      <el-table-column label="挂号单号" align="center" width="200" prop="regId"/>
      <el-table-column label="患者姓名" align="center" prop="patientName"/>
      <el-table-column label="总金额" align="center" prop="orderAmount"/>
      <el-table-column
          prop="payType"
          label="支付类型"
          align="center">
        <template slot-scope="scope">
          <el-tag
              v-if="scope.row.payType==0"
              type="success"
              effect="dark">
            现金
          </el-tag>
          <el-tag
              v-else-if="scope.row.payType==1"
              type="warning"
              effect="dark">
            支付宝
          </el-tag>
          <el-tag
              v-else
              type="info"
              effect="dark">
            微信
          </el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="订单状态" prop="orderStatus" align="center" :formatter="orderStatusFormatter"/>-->
      <el-table-column
          prop="orderStatus"
          label="订单状态"
          align="center">
        <template slot-scope="scope">
          <el-tag
              v-if="scope.row.orderStatus==1"
              type="success"
              effect="dark">
            支付成功
          </el-tag>
          <el-tag
              v-else-if="scope.row.orderStatus==3"
              type="danger"
              effect="dark">
            支付失败
          </el-tag>
          <el-tag
              v-else-if="scope.row.orderStatus==0"
              type="info"
              effect="dark">
            未支付
          </el-tag>
          <el-tag
              v-else-if="scope.row.orderStatus==2"
              type="warning"
              effect="dark">
            支付超时
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="操作" width="300px" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-view" size="mini" @click="handleViewItem(scope.row)">查看详情
          </el-button>
          <el-button type="text" :disabled="scope.row.orderStatus==='1' || scope.row.orderStatus==='2'" icon="el-icon-bank-card" size="mini"
                     @click="handlePayWithCash(scope.row)">现金收费
          </el-button>
          <el-button type="text" :disabled="scope.row.orderStatus==='1' || scope.row.orderStatus==='2'" icon="el-icon-bank-card" size="mini"
                     @click="handlePayWithZfb(scope.row)">微信收费
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页控件开始 -->
    <el-pagination
        v-show="total>0"
        :current-page="queryParams.pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
    <!-- 分页控件结束 -->
    <!-- 数据表格结束 -->
    <!-- 查看详情的弹出层开始 -->
    <el-dialog
        v-loading="loading"
        :title="title"
        :visible.sync="open"
        width="1000px"
        center
        append-to-body
    >
      <el-table
          v-loading="itemTableloading"
          border
          :data="orderChargeItemList"
      >
        <el-table-column label="详情ID" align="center" width="215" prop="itemId"/>
        <el-table-column label="处方ID" align="center" width="200" prop="coId"/>
        <el-table-column label="名称" align="center" prop="itemName"/>
        <el-table-column label="价格" align="center" prop="itemPrice"/>
        <el-table-column label="数量" align="center" prop="itemNum"/>
        <el-table-column label="小计" prop="itemAmount" align="center"/>
        <el-table-column label="类型" align="center" prop="itemType">
          <template slot-scope="scope">
            {{ scope.row.itemType==='0'?'药品':'检查' }}
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" :formatter="statusFormatter"/>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </span>
    </el-dialog>
    <!-- 查看详情的弹出层结束 -->



    <!-- 支付宝支付的弹出层开始 -->
    <el-dialog
        title="请使用微信支付"
        :visible.sync="openPay"
        center
        :close-on-click-modal="false"
        @before-close="handleClose"
        append-to-body
    >
      <el-form label-position="left" label-width="120px" inline class="demo-table-expand">
        <el-card>
          <el-form-item label="订单号:">
            <span>{{ payObj.orderNo}}</span>
          </el-form-item>
          <el-form-item label="总金额:">
            <span>{{ payObj.price }}</span>
          </el-form-item>
        </el-card>
      </el-form>
      <div style="text-align:center">
        <vue-qr :text="payObj.code_url" :size="200"/>
        <div>请使用微信扫码</div>
      </div>
    </el-dialog>
    <!-- 支付宝支付的弹出层结束 -->
  </div>
</template>

<script>
// 引入生成二维码的组件
import vueQr from 'vue-qr'
import {clear} from "core-js/internals/task";

export default {
  components: {
    vueQr
  },
  data() {
    return {

      // 是否启用遮罩层
      loading: false,
      // 详情表格的遮罩
      itemTableloading: false,
      // 分页数据总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示支付宝支付的弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        patientName: undefined,
        regId: undefined
      },
      // 数据表格的数据
      orderChargeTableList: [],
      // 支付类型字典数据
      payTypeOptions: [],
      // 订单状态字典数据
      orderStatusOptions: [],
      // 详情表格的数据
      orderChargeItemList: [],
      // 详情状态的数据字典
      statusOptions: [],
      // 支付对象
      payObj: {},
      // 是否打开支付宝二维码支付层
      openPay: false,
      // 定时轮询对象
      intervalObj: undefined,
      date:[]
    }
  },
  created() {

    /*// 查询支付类型字典数据
    this.$http.get('/system-manage/systems/dict/dept/his_pay_type_status').then(res => {
      this.payTypeOptions = res.data.data
    })*/

    /*// 订单状态字典数据
    this.$http.get('/system-manage/systems/dict/dept/his_order_charge_status').then(res => {
      this.orderStatusOptions = res.data.data
    })*/

    /*// 加载详情状态的字典数据
    this.$http.get('/system-manage/systems/dict/dept/his_order_details_status').then(res => {
      this.statusOptions = res.data.data
    })*/

    // 查询订单数据
        this.listOrderCharge()
  },
  methods: {
    listOrderCharge() {
      this.loading = true
      this.$http.post('/orderCharge/queryByPage',this.queryParams).then(res => {
        if (res.data.code === 200) {
          this.orderChargeTableList = res.data.data.list
          this.total = res.data.data.total
          this.loading = false
        } else {
          this.$message.error(res.data.msg)
          this.loading = false
        }
      })
    },

    // 条件查询
    handleQuery() {
      this.queryParams.pageNum=1
      this.listOrderCharge()
    },
    // 重置查询条件
    resetQuery() {
      this.queryParams.patientName = undefined
      this.queryParams.regId = undefined
      this.listOrderCharge()
    },
    // 分页pageSize变化时触发
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      // 重新查询
      this.listOrderCharge()
    },
    // 点击上一页  下一页，跳转到哪一页面时触发
    handleCurrentChange(val) {
      this.queryParams.pageNo = val
      // 重新查询
      this.listOrderCharge()
    },


    // 订单详情状态翻译
    statusFormatter(row) {
      let statusLabel = "未知";
      if (row.status === row.dictValue){
        statusLabel = row.dictLabel
      }
      return statusLabel
      /*this.statusOptions.filter(item => {
        if (parseInt(item.dictValue) === parseInt(row.status)) {
          statusLabel = item.dictLabel
        }
      })
      return statusLabel*/
    },
    // 查看详情
    handleViewItem(row) {
      this.title = '查询【' + row.patientName + '】支付订单详情'
      this.open = true
      this.itemTableloading = true
      // var orderId = row.orderId
      this.$http.post('/orderChargeItem/queryByOrderId?orderId='+row.orderId).then(res => {
        console.log(res.data+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        if (res.data.code === 200) {
          this.orderChargeItemList = res.data.data
          this.itemTableloading = false
        } else {
          this.$message.error(res.data.msg)
          this.itemTableloading = false
        }
      })
      // queryOrderChargeItemByOrderId(row.orderId).then(res => {
      //   this.orderChargeItemList = res.data
      //   this.itemTableloading = false
      // }).catch(() => {
      //   this.$message.error('查询失败')
      //   this.itemTableloading = false
      // })
    },
    // 关闭查看详情
    cancel() {
      this.open = false
    },
    // 现金收费
    handlePayWithCash(row) {
      this.$confirm('是否确定现金支付?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$http.get("/charge/his_order_charge/cashPay/"+row.orderId).then(res => {
          this.$message.success('现金支付成功')
          this.loading = false
          this.listOrderCharge()// 刷新列表
        }).catch(() => {
          this.$message.error('现金支付失败')
          this.loading = false
        })
      }).catch(() => {
        this.$message.error('现金支付已取消')
        this.loading = false
      })
    },
    // 支付宝收费
    handlePayWithZfb(row) {
      this.$http.post("/charge/charge/orderChargeItem/DDWxPay/"+row.orderId).then(res => {
        if (res.data.data != null){
          console.log(res.data.data)
          this.payObj = res.data.data
          this.openPay = true// 打开支付的弹出层
          this.intervalObj = setInterval(() => {
            // 轮询查询支付状态
            this.queryPayStatus(row)
          }, 3000)

        }
      })
    },
    queryPayStatus(row){
      this.$http.post("/wx-pay/queryPayStatus/"+row.orderId).then(res => {
        if (res.data.code === 200){
         this.updateOrder(row)
        }
      })

    },
    updateOrder(row){
      // 修改订单状态
      this.$http.put("/charge/charge/orderChargeItem/updateDDOrderStatus/"+row.orderId).then(res => {
      })
    },

    // 如果用户没有支付，而弹出层被关闭了
    handleClose() {
      this.$confirm('您确定放弃支付吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.warning('您已放弃支付，可以回到收费查询列表里面再次支付')
        this.openPay = false
        // 关闭轮询
        clearInterval(this.intervalObj)
      }).catch(() => {
        this.$message.info('欢迎继续支付')
      })
    }
  }
}
</script>
<style scoped>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
